//
//  ST_PecSquareHeaderView.swift
//  btvideo
//
//  Created by mac on 2020/3/18.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift
import SwiftyJSON

fileprivate let cell_shadow_radius = 3.5.cgFloat


fileprivate let cell_dis_play_shadow_offset = 8.cgFloat

class ST_PecSquareHeaderView: UICollectionReusableView {
    

    
    static let height = (ScreenWidth - 15 - 40)/320*400 + 5 + 22 + 15 + 15
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = .white
        
        
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(5)
            make.height.equalTo(25)
            make.width.equalTo(200)
        }
        
        
//        clip_view.addSubview(collectionView)
//
//
//
//        clip_view.snp.makeConstraints { (make) in
//            make.bottom.left.right.equalToSuperview()
//            make.top.equalTo(titleLabel.snp.bottom).offset(15-cell_dis_play_shadow_offset)
//        }
        
        self.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(15-cell_dis_play_shadow_offset)
            make.bottom.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
        }
        
        collectionView.addSubview(contetnView)
        contetnView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalTo(ScreenWidth)
            make.height.equalToSuperview()
        }
//        */
        
//        let v  = ST_PecSquareHeaderViewChildCell()
//        self.addSubview(v)
//        v.snp.makeConstraints { (make) in
//            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 50, left: 50, bottom: 50, right: 50))
//        }
//
//        v.cover_image_view.pin_setImage(from: "http://static.duluduludala.com/0bk336s1583549747".compress_image().url, completed: nil)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    MARK: - Properties

    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "推荐添加特质"
        control.textColor = Color.init(hexString: "#333333")
        //        control.numberOfLines = 0
        //        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.semibold)
        return control
    }()
   
    fileprivate lazy var collectionView:UIScrollView = {
        let control = UIScrollView()
//        control.delegate = self
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
//        control.isPagingEnabled = true
        control.clipsToBounds = false
        
        return control
    }()
    
    fileprivate lazy var contetnView:UIView = {
       let control = UIView()
        return control
    }()
    
    fileprivate lazy var clip_view:UIView = {
       let control = UIView()
        control.clipsToBounds = true
        return control
    }()
    
    
    //    MARK: - Bind Model
    var array = [ST_PecSquareHeaderViewChildCellModel](){
        didSet{
//            collectionView.reloadData()
            create_items()
        }
    }
    
}


//MARK:- create UICollectionView Item View
extension ST_PecSquareHeaderView{
    
    
    fileprivate func create_items(){
        
        contetnView.removeSubviews()
        
        var itemWidth = ScreenWidth - 40 - 15
        
        itemWidth += 2*cell_dis_play_shadow_offset
        
        
        let spacing = 16 - 2*cell_dis_play_shadow_offset
        
        var lastView:UIView?
        
        for model in self.array{
            
            let itemView = ST_PecSquareHeaderViewChildCell()
          
            contetnView.addSubview(itemView)
            
            itemView.snp.makeConstraints { (make) in
                if let last = lastView{
                    make.left.equalTo(last.snp.right).offset(spacing)
                }else{
                    make.left.equalToSuperview().offset(15 - cell_dis_play_shadow_offset)
                }
                make.width.equalTo(itemWidth)
                make.top.height.equalToSuperview()
            }
            
            itemView.child_model = model
            
            lastView = itemView
        }
        
        var w = (itemWidth + spacing)*self.array.count.cgFloat + 15
        
        if w < ScreenWidth{
            w = ScreenWidth
        }
        
        
        contetnView.snp.updateConstraints { (make) in
            make.width.equalTo(w)
        }
        
        
    }
    
//    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        return array.count
//    }
//
//    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//
//        let cell = collectionView.dequeueReusableCell(withClass: ST_PecSquareHeaderViewChildCell.self, for: indexPath)
//
//        cell.child_model = array[indexPath.row]
//
//        return cell
//    }
}

//MARK:- Child Cell

class ST_PecSquareHeaderViewChildCell: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        self.backgroundColor = .white
        self.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: cell_dis_play_shadow_offset, left: cell_dis_play_shadow_offset, bottom: cell_dis_play_shadow_offset, right: cell_dis_play_shadow_offset))
        }
        
        
        self.addSubview(cover_image_view)
        
        
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: cell_dis_play_shadow_offset, left: cell_dis_play_shadow_offset, bottom: cell_dis_play_shadow_offset, right: cell_dis_play_shadow_offset))
        }
        
        cover_image_view.addSubview(titleLabel)
        cover_image_view.addSubview(content_label)
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview().offset(15)
            make.height.equalTo(30)
        }
        
        
        content_label.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
        }
        
        
        cover_image_view.addSubview(visual_effect_view)
        visual_effect_view.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(85)
        }
        
        cover_image_view.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(55)
            make.centerY.equalTo(visual_effect_view)
            make.left.equalToSuperview().offset(15)
        }
        
        
        cover_image_view.addSubview(pec_title_label)
        pec_title_label.snp.makeConstraints { (make) in
            make.height.equalTo(22)
            make.top.equalTo(icon_image_view).offset(7)
            make.left.equalTo(icon_image_view.snp.right).offset(10)
        }

        cover_image_view.addSubview(pec_like_number_label)
        pec_like_number_label.snp.makeConstraints { (make) in
            make.height.equalTo(17)
            make.top.equalTo(pec_title_label.snp.bottom).offset(2)
            make.left.equalTo(pec_title_label)
        }
        
        
        self.addSubview(click_btn)
        click_btn.snp.makeConstraints { (make) in
            make.edges.equalTo(cover_image_view)
        }
        
        cover_image_view.addSubview(add_image_view)
        add_image_view.snp.makeConstraints { (make) in
            make.height.equalTo(31)
            make.width.equalTo(78)
            make.centerY.equalTo(visual_effect_view)
            make.right.equalToSuperview().offset(-15)
        }
        
        self.addSubview(add_image_view_btn)
        add_image_view_btn.snp.makeConstraints { (make) in
            make.edges.equalTo(add_image_view)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    MARK: - Properties
    fileprivate lazy var root_view = ST_RoundShadowContainerView()
    
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.clipsToBounds = true
        control.cornerRadius = 20
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = " "
        control.textColor = Color.white
        control.font = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.numberOfLines = 0
        return control
    }()
    
    fileprivate lazy var icon_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.clipsToBounds = true
        control.cornerRadius = 15
        return control
    }()
    
    fileprivate lazy var pec_title_label:UILabel = {
        let control = UILabel.init()
        control.textColor = Color.white
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var pec_like_number_label:UILabel = {
        let control = UILabel.init()
        control.textColor = Color.white
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate lazy var add_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("添加特质_alpha"))
        return control
    }()
    
    fileprivate lazy var add_image_view_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.addTarget(self, action: #selector(push_pec_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var click_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.addTarget(self, action: #selector(click_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var visual_effect_view:ST_CustomIntensityVisualEffectView = {
        let control = ST_CustomIntensityVisualEffectView.init(effect: UIBlurEffect(style: .light), intensity: 0.1)
        return control
    }()
    
    
    //    MARK:-
    var child_model = ST_PecSquareHeaderViewChildCellModel(fromJson: JSON.null){
        didSet{
            
            cover_image_view.pin_setImage(from: child_model.coverImg.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            titleLabel.text = child_model.title
            
            content_label.attributedText = child_model.remarkAttri
            
            
            
            icon_image_view.pin_setImage(from: child_model.image.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            pec_title_label.text = child_model.name
            
            var str = "个女生喜欢"
            
            if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == .female{
                str = "个男生喜欢"
            }
            
            pec_like_number_label.text = child_model.likeNum.st_number_description() + str
            
        }
    }
    
    @objc fileprivate func push_pec_handle(){
        
        
        push_peculiarity(pecId: child_model.id.string, navigationController: self.parentViewController?.navigationController, hud_controller: self.parentViewController)
        
    }
   
    @objc fileprivate func click_handle(){
        
        let vc =  ST_TodayPersonController()
        
        let m =  ST_PeculiarityModel()
        
        m.id = child_model.id
        
        m.image = child_model.image
        
        m.name = child_model.name
        
        m.type = child_model.type
        
        m.descriptionStr = child_model.remark
        
        vc.parent_peculiarity = m
        
        self.parentViewController?.navigationController?.pushViewController(vc)
        
    }
}




/**
 圆角阴影 阴影圆角
 */
fileprivate class ST_RoundShadowContainerView: UIView {
  
    let containerView = UIView()
 
    override func layoutSubviews() {
        super.layoutSubviews()
        
      // set the shadow of the view's layer
        layer.cornerRadius = 20
        layer.backgroundColor = UIColor.white.cgColor
        
        
        
        
        //        layer.shadowColor = UIColor(red: 0.61, green: 0.61, blue: 0.65, alpha: 1).cgColor
        layer.shadowColor = UIColor.init(white: 0, alpha: 0.5).cgColor
        layer.shadowOffset = CGSize(width: 0, height: 0)
        layer.shadowOpacity = 0.8
//        layer.shadowRadius = 5
        
              layer.shadowRadius = cell_shadow_radius
      
        
        
      // set the cornerRadius of the containerView's layer
//      containerView.roundCorners([UIRectCorner.topLeft,UIRectCorner.topRight], radius: 20)
//        if containerView.superview == nil{
//
//            containerView.backgroundColor = Color.white
//            insertSubview(containerView, at: 0)
//            containerView.snp.makeConstraints { (make) in
//                make.edges.equalToSuperview()
//            }
//
//        }
    }
}


